package com.facebookdirectory.entity;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.*;

/**
 * Entity implementation class for Entity: FBAppInfo
 *
 */
@Entity
public class FBAppInfo {
	   
	@Id
	private String id;
	private boolean valid = true;
	private String profilePhoto;
	private String category;
	private String profileName;
	@Column(unique=true, nullable=false) 
	private String mainURL;
	@Column(columnDefinition="text")
	private String description;
	@Column(columnDefinition="text")
	private String developer;
	private String developerName;
	@ElementCollection
	private List<String> pics = new ArrayList<String>();
	private String reviews;	
	private float averageRating = 0;
	private long monthActive;
	//private long monthActivePrev;
	private float monthActiveRate;
	private long monthActiveChange;
	private Language language;
	private boolean recommend=false;
	@Version
	private Timestamp lastUpdate;
	private Date createTime = new Date();
	
	@OneToMany(cascade=CascadeType.ALL)
	private List<FBAppUsage> statistic = new ArrayList<FBAppUsage>();

	public FBAppInfo() {
		super();
	}   
	public String getId() {
		return this.id;
	}

	public void setId(String Id) {
		this.id = Id;
	}
	/**
	 * @return the profilePhoto
	 */
	public String getProfilePhoto() {
		return profilePhoto;
	}
	/**
	 * @param profilePhoto the profilePhoto to set
	 */
	public void setProfilePhoto(String profilePhoto) {
		this.profilePhoto = profilePhoto;
	}
	/**
	 * @return the averageRating
	 */
	public float getAverageRating() {
		return averageRating;
	}
	/**
	 * @param averageRating the averageRating to set
	 */
	public void setAverageRating(float averageRating) {
		this.averageRating = averageRating;
	}
	/**
	 * @return the reviews
	 */
	public String getReviews() {
		return reviews;
	}
	/**
	 * @param reviews the reviews to set
	 */
	public void setReviews(String reviews) {
		this.reviews = reviews;
	}
	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}
	/**
	 * @param description the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}
	/**
	 * @return the developer
	 */
	public String getDeveloper() {
		return developer;
	}
	/**
	 * @param developer the developer to set
	 */
	public void setDeveloper(String developer) {
		this.developer = developer;
	}
	/**
	 * @return the developerName
	 */
	public String getDeveloperName() {
		return developerName;
	}
	/**
	 * @param developerName the developerName to set
	 */
	public void setDeveloperName(String developerName) {
		this.developerName = developerName;
	}
	/**
	 * @return the monthActive
	 */
	public long getMonthActive() {
		return monthActive;
	}
	/**
	 * @param monthActive the monthActive to set
	 */
	public void setMonthActive(long monthActive) {
		this.monthActiveChange = monthActive-this.monthActive;
		if(this.monthActive==0)
		{
			this.monthActiveRate = 0;
		}
		else
		{
			this.monthActiveRate= this.monthActiveChange/this.monthActive;
		}
		this.monthActive = monthActive;
	}
	/**
	 * @return the monthActiveChange
	 */
	public long getMonthActiveChange() {
		return monthActiveChange;
	}
	/**
	 * @param monthActiveChange the monthActiveChange to set
	 */
	protected void setMonthActiveChange(long monthActiveChange) {
		this.monthActiveChange = monthActiveChange;
	}
	/**
	 * @return the monthActiveRate
	 */
	public float getMonthActiveRate() {
		return monthActiveRate;
	}
	/**
	 * @param monthActiveRate the monthActiveRate to set
	 */
	protected void setMonthActiveRate(float monthActiveRate) {
		this.monthActiveRate = monthActiveRate;
	}
	/**
	 * @return the category
	 */
	public String getCategory() {
		return category;
	}
	/**
	 * @param category the category to set
	 */
	public void setCategory(String category) {
		this.category = category;
	}
	/**
	 * @return the profileName
	 */
	public String getProfileName() {
		return profileName;
	}
	/**
	 * @param profileName the profileName to set
	 */
	public void setProfileName(String profileName) {
		this.profileName = profileName;
	}
	/**
	 * @return the pics
	 */
	public List<String> getPics() {
		return pics;
	}
	/**
	 * @param pics the pics to set
	 */
	public void setPics(List<String> pics) {
		this.pics = pics;
	}
	/**
	 * @return the lastUpdate
	 */
	public Timestamp getLastUpdate() {
		return lastUpdate;
	}
	/**
	 * @param lastUpdate the lastUpdate to set
	 */
	public void setLastUpdate(Timestamp lastUpdate) {
		this.lastUpdate = lastUpdate;
	}
	/**
	 * @return the mainURL
	 */
	public String getMainURL() {
		return mainURL;
	}
	/**
	 * @param mainURL the mainURL to set
	 */
	public void setMainURL(String mainURL) {
		this.mainURL = mainURL;
	}
	/**
	 * @return the statistic
	 */
	public List<FBAppUsage> getStatistic() {
		return statistic;
	}
	/**
	 * @param statistic the statistic to set
	 */
	public void setStatistic(List<FBAppUsage> statistic) {
		this.statistic = statistic;
	}
	/**
	 * @return the valid
	 */
	public boolean isValid() {
		return valid;
	}
	/**
	 * @param valid the valid to set
	 */
	public void setValid(boolean valid) {
		this.valid = valid;
	}
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "FBAppInfo [id=" + id + ", valid=" + valid + ", profilePhoto="
				+ profilePhoto + ", category=" + category + ", profileName="
				+ profileName + ", mainURL=" + mainURL + ", description="
				+ description + ", developer=" + developer + ", developerName="
				+ developerName + ", pics=" + pics + ", reviews=" + reviews
				+ ", averageRating=" + averageRating + ", monthActive="
				+ monthActive + ", monthActiveRate=" + monthActiveRate
				+ ", monthActiveChange=" + monthActiveChange + ", language="
				+ language + ", lastUpdate=" + lastUpdate + ", createTime="
				+ createTime + "]";
	}
	public void assign(FBAppInfo fbAppInfo) {				
		this.setProfilePhoto(fbAppInfo.profilePhoto);
		this.setCategory(fbAppInfo.category);
		this.setProfileName(fbAppInfo.profileName);		
		this.setDescription(fbAppInfo.description);
		this.setDeveloper(fbAppInfo.developer);
		this.setDeveloperName(fbAppInfo.developerName);
		this.setPics(fbAppInfo.pics);
		this.setReviews(fbAppInfo.reviews);	
		this.setAverageRating(fbAppInfo.averageRating);
		this.setMonthActive(fbAppInfo.monthActive);		
		this.setLanguage(fbAppInfo.language);
	}
	
	//Must be called before persist
	public void updateStatistic()
	{		
		FBAppUsage usage = new FBAppUsage();
		usage.setMonthActive(monthActive);
		statistic.add(usage);
	}
	/**
	 * @return the createTime
	 */
	public Date getCreateTime() {
		return createTime;
	}
	/**
	 * @param createTime the createTime to set
	 */
	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}
	/**
	 * @return the language
	 */
	public Language getLanguage() {
		return language;
	}
	/**
	 * @param language the language to set
	 */
	public void setLanguage(Language language) {
		this.language = language;
	}
	/**
	 * @return the recommend
	 */
	public boolean isRecommend() {
		return recommend;
	}
	/**
	 * @param recommend the recommend to set
	 */
	public void setRecommend(boolean recommend) {
		this.recommend = recommend;
	}
}
